package leetcodecompetition.D358;

import CommonStructure.ListNode;

/**
 * @Author: Siantar
 * @Date: 2023-08-13-10:46
 * @Description: 1.0
 */
public class D6914 {
    public ListNode doubleIt(ListNode head) {
        head = reverse(head);
        add(head);
        head = reverse(head);
        return head;
    }

    public ListNode reverse(ListNode head) {
        ListNode p = null, pre = head;
        while (pre != null) {
            ListNode t = pre.next;
            pre.next = p;
            p = pre;
            pre = t;
        }
        return p;
    }

    public void add(ListNode head) {
        ListNode t = new ListNode();
        t.next = head;
        int carry = 0;
        while (t.next != null) {
            int v = t.next.val * 2 + carry;
            t.next.val = v % 10;
            carry = v / 10;
            t = t.next;
        }
        if (carry != 0) {
            ListNode p = new ListNode(carry);
            t.next = p;
        }
    }
}
